<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_setting_class extends AWS_CONTROLLER
{

	public function get_access_rule()
	{
		$rule_action['rule_type'] = 'white'; //黑名单,黑名单中的检查  'white'白名单,白名单以外的检查
		$rule_action['actions'] = array();
		
		return $rule_action;
	}

	function setup()
	{
		$this->crumb('设置', '/account/setting/');
	}

	function index_action()
	{
		$this->profile_action();
	}

	function profile_action()
	{		
		if ($this->user_info['birthday'] != 0) //默认的信息
		{
			TPL::assign('birthday_y_s', date('Y', $this->user_info['birthday']));
			TPL::assign('birthday_m_s', date('m', $this->user_info['birthday']));
			TPL::assign('birthday_d_s', date('d', $this->user_info['birthday']));
		}
		
		//年符值
		$year_end = date('Y');
		
		$year_array[0] = "";
		
		for ($tmp_i = $year_end; $tmp_i > 1900; $tmp_i --)
		{
			$year_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('birthday_y', $year_array);
		
		//月符值
		TPL::assign('birthday_m', array(
			"0" => "", 
			"01" => "01", 
			"02" => "02", 
			"03" => "03", 
			"04" => "04", 
			"05" => "05", 
			"06" => "06", 
			"07" => "07", 
			"08" => "08", 
			"09" => "09", 
			"10" => "10", 
			"11" => "11", 
			"12" => "12"
		));
		
		//日符值
		$day_array[0] = '';
		
		for ($tmp_i = 1; $tmp_i <= 31; $tmp_i ++)
		{
			$day_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('birthday_d', $day_array);
		
		$jobs_list_tmp = $this->model('work')->get_jos_list();
		$jobs_list[1] = "请选择";
		
		if(is_array($jobs_list_tmp))
		{
			foreach ($jobs_list_tmp as $row)
			{
				$jobs_list[$row['jobs_id']] = $row['jobs_name'];
			}
		}
		TPL::assign('jobs_list', $jobs_list);
		
		$this->crumb('基本资料', '/account/setting/profile/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js', 
			'js/LocationSelect.js'
		));
		
		TPL::output('account/setting/profile');
	}

	function profile_process_ajax_action()
	{	
		$update_data['province'] = $_POST['province'];
		$update_data['city'] = $_POST['city'];
		$update_data['district'] = $_POST['district'];
		$update_data['job_id'] = intval($_POST['job_id']);
		
		$user_id = $this->user_id;
		$user_info = $this->user_info;
		
		if (!$user_info['user_name'] OR $user_info['user_name'] == $user_info['email'])
		{
			$update_data['user_name'] = htmlspecialchars(trim($_POST['user_name']));
			
			if (! $this->model('account')->check_username_char($update_data['user_name']))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'user_name'
				), '-1', "真实姓名请输入 2-7 个汉字，或 4-14 个英文"));
			}
		}
		
		if ($this->model('account')->check_username($update_data['user_name']) and $user_info['user_name'] != $update_data['user_name'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'user_name'
			), '-1', '已经存在相同的姓名, 请重新填写'));
		}
		
		if (! H::valid_email($user_info['email']))
		{
			if (! H::valid_email($_POST['email']))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'email'
				), '-1', '请输入正确的 E-Mail 地址'));
			}
			
			if ($this->model('account')->check_email($_POST['email']))
			{
				H::ajax_json_output(AWS_APP::RSM(array(
					'input' => 'email'
				), '-1', '邮箱已经存在, 请使用新的邮箱'));
			}
			
			$update_data['email'] = $_POST['email'];
		
			$this->model('active')->new_valid_email($this->user_id, $_POST['email']);
		}
		
		$update_data['sex'] = intval($_POST['sex']);
		
		if ($_POST['birthday_y'])
		{
			$update_data['birthday'] = intval(strtotime(intval($_POST['birthday_y']) . '-' . intval($_POST['birthday_m']) . '-' . intval($_POST['birthday_d'])));
		}
		
		$update_attrib_arr['signature'] = htmlspecialchars($_POST['signature']);		

		//更新主表
		$this->model('account')->update_users_fields($update_data, $this->user_id);
		
		//更新从表
		$this->model('account')->update_users_attrib_fields($update_attrib_arr, $this->user_id);
		
		ZCACHE::cleanGroup('g_uid_' . $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '个人资料已更新'));
	
	}

	/**
	 * 修改联系方式
	 */
	function contact_action()
	{
		$this->crumb('联系方式', '/account/setting/contact/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output("account/setting/contact");
	}

	function contact_process_ajax_action()
	{	
		$update_attrib_arr['qq'] = intval($_POST['qq']);
		$update_attrib_arr['msn'] = htmlspecialchars(trim($_POST['msn']));
		$update_attrib_arr['popular_email'] = htmlspecialchars(trim($_POST['popular_email']));		
		$update_attrib_arr['homepage'] = htmlspecialchars(trim($_POST['homepage']));		
		$update_data['mobile'] = htmlspecialchars(trim($_POST['mobile']));
		
		//联系方式添加积分
		/*if ($update_attrib_arr['qq'] || $update_attrib_arr['msn'] || $update_attrib_arr['popular_email'] || $update_attrib_arr['homepage'] || $update_data['mobile'])
		{
			$this->model("integral")->set_user_integral('FILL_CONTACT', $this->user_id, "你的个人设置补充了联系方式");
		}*/
		
		//更新主表
		$this->model('account')->update_users_fields($update_data, $this->user_id);
		
		//更新从表
		$this->model('account')->update_users_attrib_fields($update_attrib_arr, $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, '联系资料已更新'));
	
	}

	/**
	 * 私信设置
	 */
	function inbox_action()
	{
		$email_setting = $this->model('account')->get_email_setting_by_uid($this->user_id);
		
		$nt_setting = $this->model('account')->get_notification_setting_by_uid($this->user_id);
		
		TPL::assign('email_setting', $email_setting);
		TPL::assign('nt_setting', $nt_setting);
		TPL::assign('notify_actions', $this->model('notify')->notify_action_details);
		
		$this->crumb('私信及邮件通知', '/account/setting/inbox/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output("account/setting/inbox");
	}

	function inbox_process_ajax()
	{
		$update_data['inbox_recv'] = intval($_POST['inbox_recv']);
		
		//更新主表
		$this->model('account')->update_users_fields($update_data, $this->user_id);
		
		$update_dataay['sender_11'] = intval($_POST['sender_11']);
		$update_dataay['sender_12'] = intval($_POST['sender_12']);
		$update_dataay['sender_13'] = intval($_POST['sender_13']);
		$update_dataay['sender_14'] = intval($_POST['sender_14']);
		$update_dataay['sender_15'] = intval($_POST['sender_15']);
		
		$nt_setting = $_POST['nt_setting'];
		
		if ($notify_actions = $this->model('notify')->notify_action_details)
		{
			
			foreach ($notify_actions as $key => $val)
			{
				if (! isset($nt_setting[$key]) && $val['user_setting'])
				{
					$nt_data[] = $key;
				}
			}
		}
		$update_nt_array = array(
			'data' => serialize($nt_data)
		);
		
		$this->model('account')->update_notification_setting_fields($update_nt_array, $this->user_id);
		$this->model('account')->update_email_setting_fields($update_dataay, $this->user_id);
		
		$this->model('account')->update_users_fields(array(
			'weibo_visit' => intval($_POST['weibo_visit'])
		), $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, "设置已保存"));
	}

	
	function password_action()
	{
		$this->crumb('修改密码', '/account/setting/assword/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('account/setting/password');
	}

	function editpassword_process_ajax_action()
	{		
		$old_password = $_POST['old_password'];
		$password = $_POST['password'];
		$re_password = $_POST['re_password'];
		
		if (!$old_password)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'old_password'
			), '-1', '请输入当前密码'));
		}
		
		if ($password != $re_password)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'password'
			), '-1', '请输入相同的确认密码'));
		}
		
		if (strlen($password) < 6 OR strlen($password) > 16)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'password'
			), -1, "密码长度不符合规则"));
		}
		
		if (get_setting('ucenter_enabled') == 'Y')
		{
			if ($this->model('ucenter')->is_uc_user($this->user_info['email']))
			{
				$result = $this->model('ucenter')->user_edit($this->user_id, $this->user_info['user_name'], $old_password, $password);
				
				if ($result !== 1)
				{
					H::ajax_json_output(AWS_APP::RSM(null, -1, $result));
				}
			}
		}
		
		if ($this->model('account')->update_user_password($old_password, $password, $this->user_id, $this->user_info['salt']))
		{
			H::ajax_json_output(AWS_APP::RSM(null, 1, '密码修改成功, 请牢记新密码'));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'old_password'
			), '-1', '请输入正确的当前密码'));
		}
	}

	/*******教育************************************************************************************************/
	
	/**
	 * 教育信息
	 */
	function edu_action()
	{
		$education_experience_list = $this->model('education')->get_education_experience_list($this->user_id);
		
		TPL::assign('education_experience_list', $education_experience_list);
		
		$year_end = date('Y');
		
		$year_array[0] = '请选择';
		
		for ($tmp_i = $year_end; $tmp_i > 1900; $tmp_i --)
		{
			$year_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('education_years_list', $year_array);
		
		$this->crumb('教育经历', '/account/setting/edu/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output('account/setting/edu');
	}

	function edu_edit_action()
	{
		$education_id = intval($_GET['eid']);
		
		$education_experience_row = $this->model('education')->get_education_experience_row($education_id, $this->user_id);
		
		if (! $education_experience_row)
		{
			H::redirect_msg("没有找到相关教育信息");
		}
		
		TPL::assign('education_experience_row', $education_experience_row);
		
		$year_end = date('Y');
		
		$year_array[0] = '请选择';
		
		for ($tmp_i = $year_end; $tmp_i > 1900; $tmp_i --)
		{
			$year_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('school_type_list', array(
			5 => '大学', 
			4 => '高中', 
			3 => '中技', 
			2 => '初中', 
			1 => '小学'
		));
		
		TPL::assign('school_type', $education_experience_row['school_type']);
		TPL::assign('education_years', $education_experience_row['education_years']);
		TPL::assign('education_years_list', $year_array);
		
		$this->crumb('教育经历', '/account/setting/edu/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output("account/setting/edu_edit");
	}
	
	//添加教育经历 
	function add_edu_process_ajax_action()
	{
		$school_type = intval($_POST['school_type']);
		$school_name = htmlspecialchars(trim($_POST['school_name']));
		$education_years =intval($_POST['education_years']);
		$departments = htmlspecialchars($_POST['departments']);
		
		if (empty($_POST['school_name']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'school_name'
			), '-1', "请输入学校名称"));
		}
		
		if (empty($_POST['departments']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'departments'
			), '-1', "请输入院系"));
		}
		
		if ($_POST['education_years'] == "请选择" OR !$_POST['education_years'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'education_years'
			), '-1', "请选择入学年份"));
		}
		
		$this->model('education')->add_education_experience($this->user_id, $school_type, $school_name, $education_years, $departments);
		
		//添加积分
		//$this->model('integral')->set_user_integral('FILL_EDU', $this->user_id, '你的个人设置补充了教育经历');
			
		H::ajax_json_output(AWS_APP::RSM(null, 1, "教育经历添加成功")); //格式化输出,并转为JSON进行处理
	
	}
	
	//修改教育经历 
	function edit_edu_process_ajax_action()
	{
		if (empty($_POST['school_name']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'school_name'
			), '-1', "请输入学校名称"));
		}
		
		if (empty($_POST['departments']))
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'departments'
			), '-1', "请输入院系"));
		}
		
		if ($_POST['education_years'] == "请选择" OR !$_POST['education_years'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'education_years'
			), '-1', "请选择入学年份"));
		}
		
		$update_data['school_type'] = intval($_POST['school_type']);
		$update_data['school_name'] = htmlspecialchars($_POST['school_name']);
		$update_data['education_years'] = intval($_POST['education_years']);
		$update_data['departments'] = htmlspecialchars($_POST['departments']);		
		$education_id = intval($_POST['eid']);
		
		$this->model('education')->update_education_experience($update_data, $education_id, $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(array(
			'url' => get_js_url('/account/setting/edu/')
		), 1, null));
		
	}
	
	// 删除教育经历
	function edu_remove_ajax_action()
	{
		$this->model('education')->del_education_experience(intval($_GET['eid']), $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, null));
	
	}

	/*************工作经历********************************************************************************************************/
	
	/**
	 * 工作经历
	 */
	
	function work_action()
	{
	
		if ($jobs_list_tmp = $this->model('work')->get_jos_list())
		{
			$jobs_list[0] = "";			
			foreach ($jobs_list_tmp as $row)
			{
				$jobs_list[$row['jobs_id']] = $row['jobs_name'];
			}
		}
		
		if ($work_experience_list = $this->model('work')->get_work_experience_list($this->user_id))
		{
			foreach ($work_experience_list as $key => $val)
			{
				$work_experience_list[$key]['jobs_name'] = $jobs_list[$val['jobs_id']];
			}
		}
		
		TPL::assign('jobs_list', $jobs_list);
		TPL::assign('work_experience_list', $work_experience_list);
		
		$year_end = date('Y');
		
		$year_array[0] = '';
		
		for ($tmp_i = $year_end; $tmp_i > 1900; $tmp_i --)
		{
			$year_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('month_list', array(
			0 => '', 
			1 => 1, 
			2 => 2, 
			3 => 3, 
			4 => 4, 
			5 => 5, 
			6 => 6, 
			7 => 7, 
			8 => 8, 
			9 => 9, 
			10 => 10, 
			11 => 11, 
			12 => 12
		));
		
		TPL::assign('years_list', $year_array);
		
		$this->crumb('工作经历', '/account/setting/work/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js', 
			'js/LocationSelect.js'
		));
		
		TPL::output("account/setting/work");
	}
	
	//添加工作经历 
	function add_work_process_ajax_action()
	{
		$jobs_id = intval($_POST['jobs_id']);
		$company_name = htmlspecialchars($_POST['company_name']);
		$province = $_POST['province'];
		$city = $_POST['city'];
		$district = $_POST['district'];
		
		$start_year = intval($_POST['start_year']);
		$start_month = intval($_POST['start_month']);
		$end_year = intval($_POST['end_year']);
		$end_month = intval($_POST['end_month']);
		
		if (!$company_name)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'company_name'
			), '-1', "请输入公司名称"));
		}
		
		if (!$province OR !$city OR !$district)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'province'
			), '-1', "请选择工作地点"));
		}
		
		if (!$jobs_id)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'jobs_id'
			), '-1', "请选择职位"));
		}
		
		if (!$start_year OR !$start_month OR !$end_year OR !$end_month)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'start_year'
			), '-1', "请选择工作时间"));
		}
		
		$this->model('work')->add_work_experience($this->user_id, $start_year, $start_month, $end_year, $end_month, $company_name, '', $jobs_id, 0, $province, $city, $district);
		
		//添加积分
		//$this->model("integral")->set_user_integral("FILL_CAREER", $this->user_id, "补充工作经历");
			
		H::ajax_json_output(AWS_APP::RSM(null, 1, "工作经历添加成功"));
	}
	
	// 删除工作经历
	function work_remove_ajax_action()
	{
		$this->model('work')->del_work_experience($_GET['wid'], $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, 1, null));
	}

	function work_edit_action()
	{
		$work_id = intval($_GET['wid']);
		
		$work_experience_row = $this->model('work')->get_work_experience_row($work_id, $this->user_id);
		
		if (! $work_experience_row)
		{
			H::redirect_msg("没有找到相关工作经历");
		}
		
		TPL::assign('work_experience_row', $work_experience_row);
		
		if ($jobs_list_tmp = $this->model('work')->get_jos_list())
		{
			$jobs_list[0] = '';
			
			foreach ($jobs_list_tmp as $row)
			{
				$jobs_list[$row['jobs_id']] = $row['jobs_name'];
			}
		}
		
		TPL::assign('jobs_list', $jobs_list);
		TPL::assign('jobs_list_select', $work_experience_row['jobs_id']);
		
		TPL::assign('start_year', $work_experience_row['start_year']);
		TPL::assign('end_year', $work_experience_row['end_year']);
		TPL::assign('start_month', $work_experience_row['start_month']);
		TPL::assign('end_month', $work_experience_row['end_month']);
		
		$year_end = date('Y');
		
		$year_array[0] = '';
		
		for ($tmp_i = $year_end; $tmp_i > 1900; $tmp_i --)
		{
			$year_array[$tmp_i] = $tmp_i;
		}
		
		TPL::assign('month_list', array(
			0 => '', 
			1 => 1, 
			2 => 2, 
			3 => 3, 
			4 => 4, 
			5 => 5, 
			6 => 6, 
			7 => 7, 
			8 => 8, 
			9 => 9, 
			10 => 10, 
			11 => 11, 
			12 => 12
		));
		
		TPL::assign('years_list', $year_array);
		
		$this->crumb('工作经历', '/account/setting/work/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::import_js(array(
			'js/ajaxupload.js', 
			'js/LocationSelect.js'
		));
		
		TPL::output("account/setting/work_edit");
	}
	
	//修改工作经历 
	function edit_work_process_ajax_action()
	{
		if (!$_POST['company_name'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'company_name'
			), '-1', "请输入公司名称"));
		}
		
		if (!$_POST['province'] OR !$_POST['city'] OR !$_POST['district'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'province'
			), '-1', "请选择工作地点"));
		}
		
		if (!$_POST['jobs_id'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'jobs_id'
			), '-1', "请选择职位"));
		}
		
		if (!$_POST['start_year'] OR !$_POST['start_month'] OR !$_POST['end_year'] OR !$_POST['end_month'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'input' => 'start_year'
			), '-1', "请选择工作时间"));
		}

		$update_data['jobs_id'] = intval($_POST['jobs_id']);
		$update_data['company_name'] = htmlspecialchars($_POST['company_name']);
		$update_data['province'] = $_POST['province'];
		$update_data['city'] = $_POST['city'];
		$update_data['district'] = $_POST['district'];
		
		$update_data['start_year'] = intval($_POST['start_year']);
		$update_data['start_month'] = intval($_POST['start_month']);
		$update_data['end_year'] =  intval($_POST['end_year']);
		$update_data['end_month'] = intval($_POST['end_month']);
		
		$work_id = intval($_POST['wid']);
		
		$this->model('work')->update_work_experience($update_data, $work_id, $this->user_id);
		
		H::ajax_json_output(AWS_APP::RSM(array('url' => get_js_url('/account/setting/work/')), 1, "工作经历修改成功"));
	}

	
	function accountbind_action()
	{
		$sina_weibo = $this->model('sina_weibo')->get_users_sina_by_uid($this->user_id);
		$qq_weibo = $this->model('qq_weibo')->get_users_qq_by_uid($this->user_id);
		
		TPL::assign('sina_weibo', $sina_weibo);
		TPL::assign('qq_weibo', $qq_weibo);
		
		$this->crumb('微博绑定', '/account/setting/accountbind/');
		
		TPL::import_css(array(
			'css/discussion.css'
		));
		
		TPL::output("account/setting/accountbind");
	}
}
